package org.jtao.gbsubject.web.rest;

import com.codahale.metrics.annotation.Timed;
import org.jtao.gbsubject.service.TopSubjectService;
import org.jtao.gbsubject.web.rest.errors.BadRequestAlertException;
import org.jtao.gbsubject.web.rest.util.HeaderUtil;
import org.jtao.gbsubject.web.rest.util.PaginationUtil;
import org.jtao.gbsubject.service.dto.TopSubjectDTO;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.net.URI;
import java.net.URISyntaxException;

import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing TopSubject.
 */
@RestController
@RequestMapping("/api")
public class TopSubjectResource {

    private final Logger log = LoggerFactory.getLogger(TopSubjectResource.class);

    private static final String ENTITY_NAME = "topSubject";

    private final TopSubjectService topSubjectService;

    public TopSubjectResource(TopSubjectService topSubjectService) {
        this.topSubjectService = topSubjectService;
    }

    /**
     * POST  /top-subjects : Create a new topSubject.
     *
     * @param topSubjectDTO the topSubjectDTO to create
     * @return the ResponseEntity with status 201 (Created) and with body the new topSubjectDTO, or with status 400 (Bad Request) if the topSubject has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/top-subjects")
    @Timed
    public ResponseEntity<TopSubjectDTO> createTopSubject(@RequestBody TopSubjectDTO topSubjectDTO) throws URISyntaxException {
        log.debug("REST request to save TopSubject : {}", topSubjectDTO);
        if (topSubjectDTO.getId() != null) {
            throw new BadRequestAlertException("A new topSubject cannot already have an ID", ENTITY_NAME, "idexists");
        }
        TopSubjectDTO result = topSubjectService.save(topSubjectDTO);
        return ResponseEntity.created(new URI("/api/top-subjects/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }


    @PostMapping("/top-subjects/import")
    @Timed
    public ResponseEntity<Integer> importColleges(@RequestParam("file") MultipartFile file) {
        log.debug("REST request to import Colleges : {} ",file.getOriginalFilename());
        Integer sum=topSubjectService.importColleges(file);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityImportAlert(ENTITY_NAME,sum.toString()))
            .body(sum);
    }

    /**
     * PUT  /top-subjects : Updates an existing topSubject.
     *
     * @param topSubjectDTO the topSubjectDTO to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated topSubjectDTO,
     * or with status 400 (Bad Request) if the topSubjectDTO is not valid,
     * or with status 500 (Internal Server Error) if the topSubjectDTO couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/top-subjects")
    @Timed
    public ResponseEntity<TopSubjectDTO> updateTopSubject(@RequestBody TopSubjectDTO topSubjectDTO) throws URISyntaxException {
        log.debug("REST request to update TopSubject : {}", topSubjectDTO);
        if (topSubjectDTO.getId() == null) {
            return createTopSubject(topSubjectDTO);
        }
        TopSubjectDTO result = topSubjectService.save(topSubjectDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, topSubjectDTO.getId().toString()))
            .body(result);
    }

    /**
     * GET  /top-subjects : get all the topSubjects.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of topSubjects in body
     */
    @GetMapping("/top-subjects")
    @Timed
    public ResponseEntity<List<TopSubjectDTO>> getAllTopSubjects(Pageable pageable) {
        log.debug("REST request to get a page of TopSubjects");
        Page<TopSubjectDTO> page = topSubjectService.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/top-subjects");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /top-subjects/:id : get the "id" topSubject.
     *
     * @param id the id of the topSubjectDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the topSubjectDTO, or with status 404 (Not Found)
     */
    @GetMapping("/top-subjects/{id}")
    @Timed
    public ResponseEntity<TopSubjectDTO> getTopSubject(@PathVariable Long id) {
        log.debug("REST request to get TopSubject : {}", id);
        TopSubjectDTO topSubjectDTO = topSubjectService.findOne(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(topSubjectDTO));
    }

    /**
     * DELETE  /top-subjects/:id : delete the "id" topSubject.
     *
     * @param id the id of the topSubjectDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/top-subjects/{id}")
    @Timed
    public ResponseEntity<Void> deleteTopSubject(@PathVariable Long id) {
        log.debug("REST request to delete TopSubject : {}", id);
        topSubjectService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }
}
